//
//  BViewController.m
//  ZhiBoYuan
//
//  Created by mac on 2018/4/12.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "BViewController.h"
#import "ASubViewController.h"

#import "BCellTableViewCell.h"

#import "BSDetailViewController.h"

#import "TaskTeachVC.h"


static NSString *ident = @"BCellTableViewCell";

@interface BViewController ()

@property (nonatomic, copy)NSMutableArray *listArray;

@property (nonatomic, assign)int page;

@end

@implementation BViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    _page = 1;
    
    _listArray = [NSMutableArray array];
    
    [self getNewData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [Helper setMoRenNav:self.navigationController];
    [Helper setNavColorFrom:self.navigationController color:ZhuTiColor];
    
    [self.view addSubview:self.tableView];
}

//-(void)open{
//    TaskTeachVC *vc = [[TaskTeachVC alloc]init];
//
//    vc.hidesBottomBarWhenPushed = YES;
//
//    [self.navigationController pushViewController:vc animated:YES];
//}

#pragma mark -----------------------获取数据------------------------
-(void)getNewData{
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    
    parameter.body = @{@"page":[NSString stringWithFormat:@"%d",_page],
                       @"page_count":@"10"
                       };
    
    NSString *lastUrl  = @"g=Client&m=Task&a=taskList";
    [SGHttpTool POST:[NSString stringWithFormat:@"%@%@",HTTPP,lastUrl] parameter:parameter sucess:^(id responseObject) {
        
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        
        if (_page == 1) {
            [_listArray removeAllObjects];
        }
        
        NSDictionary *dic = responseObject;
        NSLog(@"%@",dic);
        
        if ([dic[@"status"] intValue] == 200) {
            
            if([dic[@"data"][@"task_list"] isEqual:[NSNull null]]){
                NSLog(@"KDA!----%d-----",_page);
                
                
            }else{
                for (NSDictionary *dicc in dic[@"data"][@"task_list"]) {
                    [_listArray addObject:dicc];
                }
                
                if (_listArray.count == 0) {
                    _tableView.mj_footer.hidden = YES;
                }else{
                    _tableView.mj_footer.hidden = NO;
                }
                
                [self.tableView reloadData];
                
            }
            
            
        }else{
            
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (_listArray.count == 0) {
        
    }else{
        
        if ([[_listArray[indexPath.row] valueForKey:@"had_view"] intValue]==0) {
            [cell.weichakan setImage:[UIImage imageNamed:@"未查看副本"]];
        }else{
            [cell.weichakan setImage:[UIImage imageNamed:@"已查看"]];
        }
        
        cell.banjiNameLable.text = [_listArray[indexPath.row][@"class_info"] valueForKey:@"name"];
        cell.banjiDetail.text = [_listArray[indexPath.row] valueForKey:@"name"];
        cell.renwushijian.text = [_listArray[indexPath.row] valueForKey:@"work_time"];
        NSString *url = [_listArray[indexPath.row][@"teacher_info"] valueForKey:@"small_img"];
        [cell.teacherHeadImage sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"图层18"]];
        cell.teacherNameLable.text = [_listArray[indexPath.row][@"teacher_info"] valueForKey:@"name"];
        cell.teacherYaoQiuLable.text = [_listArray[indexPath.row] valueForKey:@"detail"];
        
        
    }
    
    return cell;
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_listArray.count > 0) {
        
        BSDetailViewController *vc = [[BSDetailViewController alloc]init];
        vc.renwuId = [_listArray[indexPath.row] valueForKey:@"id"];
        //    NSLog(@"%@",[_listArray[indexPath.row] valueForKey:@"count"]);
        vc.FaBiaoNum = [_listArray[indexPath.row] valueForKey:@"count"];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
}



#pragma makr -- property
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (AxcEmptyDataTableView *)tableView {
    if (!_tableView) {
        AxcEmptyDataTableView *  tableView = [[AxcEmptyDataTableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight- NAVIGATION_BAR_HEIGHT -TAB_BAR_HEIGHT) style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate        = self;
        tableView.dataSource      = self;
        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
        
        tableView.rowHeight = UITableViewAutomaticDimension;//iOS8之后默认就是这个值，可以省略
        
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        
        //        self.extendedLayoutIncludesOpaqueBars = YES;
        
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
        tableView.scrollIndicatorInsets = _tableView.contentInset;
        
        
        [tableView registerNib:[UINib nibWithNibName:ident bundle:nil] forCellReuseIdentifier:ident];
        
        //        [tableView registerClass:[WoTableViewCell class] forCellReuseIdentifier:identfire];
        //        [tableView registerClass:[WoHeaderCell class] forCellReuseIdentifier:identHead];
        
        
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        __weak __typeof(&*self)weakSelf = self;
        
        // 下拉刷新
        tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            _page = 1;
            
            [self getNewData];
            
        }];
        
        //         上拉加载
        tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            _page = _page + 1;
            
            [self getNewData];
            
        }];
        
        
        self.placeholderView.frame = tableView.bounds;
        
        // 设置空集时候展示的View
        tableView.axcUI_placeHolderView = self.placeholderView;
        
        //  默认YES 自带渐入渐出动画效果，下边这个参数可以关闭
        tableView.axcUI_placeHolderViewAnimations = NO;
        
        // 占位View的“重新加载”按钮的Block回调:
        __weak typeof(self) WeakSelf = self;
        
        self.placeholderView.clickLoadBtnBlock = ^(id type) {
            //        WeakSelf.dataArray = nil;
            
            _page = 1;
            
            [weakSelf getNewData];
            
            
        };
        
        _tableView = tableView;
        
        
        
        
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

